package com.sj.purchase.modules.cost.config.task.synchronizedata;

import com.alibaba.fastjson.JSON;
import com.sj.purchase.modules.gy.repository.GySpuRepository;
import com.sj.purchase.modules.gy.domain.GySpu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by yangrd on 2017/7/26.
 */
@Component
public class GySpuTask {

    @Value("${sj.darwin.synchronize}")
    private String darwinApi;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private GySpuRepository gySpuRepository;

    /**
     * @param day yyyy/MM/dd
     */
    @Transactional
    public void synchronizeGySpu(String day) {
        String jsonArray = restTemplate.getForObject(darwinApi + day, String.class);
        List<GySpu> gySpuList = JSON.parseArray(jsonArray, GySpu.class);
        gySpuRepository.save(gySpuList);
    }
}
